# encoding: utf-8
from app.extensions import log, db
from flask_restplus import Resource
from app.extensions.api import Namespace
from app.modules.common.utils import DbCommit, checkToken
from . import parameters, schemas
from .models import ZoneInfo
from app.modules.common.schemas import BaseSchema

api = Namespace('zones', description = "区域")  # pylint: disable=invalid-name


@api.route('/')
class GetZoneList(Resource):
    @api.parameters(parameters.ZoneListParameters())
    @api.response(schemas.ZoneListSchema(many = False))
    @checkToken
    def get(self, args, **kwargs):
        """
        获取区域列表
        :param args:
        :return:
        """
        _env_name = args['env_name']
        _zone_code = args['zone_code']
        _query = ZoneInfo.query.filter_by(env_name = _env_name)
        if _zone_code is not None:
            _query = _query.filter_by(zone_code = _zone_code)

        _list = _query.all()

        return {'success': True, 'msg': '', 'data': _list}


@api.route('/add')
class AddZone(Resource):
    @api.parameters(parameters.ZoneAddParameters())
    @api.response(BaseSchema(many = False))
    @checkToken
    def post(self, args, **kwargs):
        """
        新增区域
        :param args:
        :return:
        """
        _env_name = args['env_name']
        _zone_code = args['zone_code']
        _zone_alias = args['zone_alias']
        _zone_title = args['zone_title']
        _parent_code = args['parent_code']

        _info = ZoneInfo.query.filter_by(env_name = _env_name, zone_code = _zone_code).first()
        if _info is not None:
            _msg = "环境 %s 的区域code %s 已存在" % (_env_name, _zone_code)
            log.error("============== %s ==============" % _msg)
            return {'success': False, 'msg': _msg}

        db.session.add(
            ZoneInfo(env_name = _env_name, zone_code = _zone_code, zone_alias = _zone_alias, zone_title = _zone_title,
                     parent_code = _parent_code))

        return DbCommit()


@api.route('/del')
class DelZone(Resource):
    @api.parameters(parameters.ZoneDelParameters())
    @api.response(BaseSchema(many = False))
    @checkToken
    def post(self, args, **kwargs):
        """
        删除区域
        :param args:
        :return:
        """
        _env_name = args['env_name']
        _zone_code = args['zone_code']

        _info = ZoneInfo.query.filter_by(env_name = _env_name, zone_code = _zone_code).first()
        if _info is None:
            _msg = "环境 %s 的区域code %s 不存在" % (_env_name, _zone_code)
            log.error("============== %s ==============" % _msg)
            return {'success': False, 'msg': _msg}

        db.session.delete(_info)

        return DbCommit()


@api.route('/update')
class UpdateZone(Resource):
    @api.parameters(parameters.ZoneAddParameters())
    @api.response(BaseSchema(many = False))
    @checkToken
    def post(self, args, **kwargs):
        """
        修改区域
        :param args:
        :return:
        """
        _env_name = args['env_name']
        _zone_code = args['zone_code']
        _zone_alias = args['zone_alias']
        _zone_title = args['zone_title']
        _parent_code = args['parent_code']

        _info = ZoneInfo.query.filter_by(env_name = _env_name, zone_code = _zone_code).first()
        if _info is None:
            _msg = "环境 %s 区域code %s 不存在" % (_env_name, _zone_code)
            log.error("============== %s ==============" % _msg)
            return {'success': False, 'msg': _msg}

        _info.zone_alias = _zone_alias
        _info.zone_title = _zone_title
        _info.parent_code = _parent_code

        return DbCommit()


@api.route('/updateOperateGroup')
class UpdateOperateGroup(Resource):
    @api.parameters(parameters.UpdateOperateGroupParameters())
    @api.response(BaseSchema(many = False))
    @checkToken
    def post(self, args, **kwargs):
        """
        修改区域当前操作的分组
        :param args:
        :return:
        """
        _env_name = args['env_name']
        _zone_code = args['zone_code']
        _operate_group = args['operate_group']

        _info = ZoneInfo.query.filter_by(env_name = _env_name, zone_code = _zone_code).first()
        if _info is None:
            _msg = "环境 %s 区域code %s 不存在" % (_env_name, _zone_code)
            log.error("============== %s ==============" % _msg)
            return {'success': False, 'msg': _msg}

        if _info.check_status != 'READY':
            _msg = "只有区域校验成功后才能进行切换操作"
            log.error("============== %s ==============" % _msg)
            return {'success': False, 'msg': _msg}

        _info.operate_group = _operate_group
        if _operate_group == 'NONE':
            _info.check_status = None

        return DbCommit()
